﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.DTO;
using QL_DTTN.DTO.DTO;
using QL_DTTN.TTTS.BUS;
using DevExpress.XtraGrid.Views.Grid;
namespace QL_DTTN.TTTS
{
    public partial class FrmImportThiSinh : DevExpress.XtraEditors.XtraForm
    {
        List<ThiSinh> _thiSinh;
        public FrmImportThiSinh()
        {
            InitializeComponent();
            _thiSinh = new List<ThiSinh>();
        }
        private void FrmImportThiSinh_Load(object sender, EventArgs e)
        {
            cbxKyThi_Load();
            cbxTinh_Load();
            loadConTrol();
            gctThiSinh.DataSource = _thiSinh;
        }
        private void btnChonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofile = new OpenFileDialog();
            txtDuongDan.Text = ofile.ShowDialog() == DialogResult.OK ? ofile.FileName : "";
            if (txtDuongDan.Text == "")
            {
                return;
            }
            loadGrid();
        }
        private void loadGrid()
        {
            DataTable dt = ExcelDataProvider.ReadDataFromExcelFile(txtDuongDan.Text.Trim());
            _thiSinh.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ThiSinh ts = new ThiSinh();
                ts.HoTen = dt.Rows[i][0].ToString();
                ts.GioiTinh = Convert.ToBoolean(dt.Rows[i][1]);
                ts.DiaChi = dt.Rows[i][2].ToString();
                ts.NgaySinh = Convert.ToDateTime(dt.Rows[i][3]);
                if (dt.Rows[i][4].ToString() != "")
                {
                    ts.NgayVaoDang = Convert.ToDateTime(dt.Rows[i][4]);
                }
                if (dt.Rows[i][5].ToString() != "")
                {
                    ts.NgayVaoDoan = Convert.ToDateTime(dt.Rows[i][5]);
                }
                ts.SoCMND = dt.Rows[i][6].ToString();
                ts.MaDanToc = Convert.ToInt16(dt.Rows[i][7]);
                ts.MaHeDaoTao = Convert.ToInt16(dt.Rows[i][8]);
                ts.MaTinh = Convert.ToInt16(dt.Rows[i][9]);
                ts.MaDoiTuong = dt.Rows[i][10].ToString();
                _thiSinh.Add(ts);
            }
            gvwThiSinh.RefreshData();
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            long maKT = Convert.ToInt64(cbxKyThi.SelectedValue);
            string maTruong = cbxTruong.SelectedValue.ToString();
            int maID = ThiSinh_Bus.getMaxID(DateTime.Now.Year.ToString());
            if (maID<0)
            {
                return;
            }
            foreach (var item in _thiSinh)
            {
                item.MaKyThi = maKT;
                item.MaTruong = maTruong;
                item.SoBaoDanh = ThiSinh_Bus.getID(DateTime.Now.Year.ToString(),++ maID);
            }
            if (ThiSinh_Bus.Them(_thiSinh))
            {
                XtraMessageBox.Show("Thành công");
                txtDuongDan.Text = "";
                _thiSinh.Clear();
                gvwThiSinh.RefreshData();
            }
            else
            {
                XtraMessageBox.Show("Thất bại");
            }

        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn có chắc muốn thoát không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult == DialogResult.OK)
            {
                this.Close();
            }
        }
        private void cbxTinh_Load()
        {
            cbxTinh.DataSource = Tinh_Bus.dataTable();
            cbxTinh.ValueMember = "maTinh";
            cbxTinh.DisplayMember = "tenTinh";
        }
        private void cbxKyThi_Load()
        {
            cbxKyThi.DataSource = KyThi_Bus.dataTable();
            cbxKyThi.ValueMember = "maKyThi";
            cbxKyThi.DisplayMember = "tenKyThi";
        }
        private void loadConTrol()
        {
            iluDanToc.DataSource = DanToc_Bus.dataTable();
            iluDanToc.DisplayMember = "tenDanToc";
            iluDanToc.ValueMember = "maDanToc";
            iluTinh.DataSource = Tinh_Bus.dataTable();
            iluTinh.DisplayMember = "tenTinh";
            iluTinh.ValueMember = "maTinh";
            iluHeDaoTao.DataSource = HeDaoTao_Bus.dataTable();
            iluHeDaoTao.DisplayMember = "tenHeDaoTao";
            iluHeDaoTao.ValueMember = "maHeDaoTao";
            iluDoiTuong.DataSource = DoiTuong_Bus.dataTable();
            iluDoiTuong.DisplayMember = "tenDoiTuong";
            iluDoiTuong.ValueMember = "maDoiTuong";
        }
        private void cbxTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxTinh.SelectedIndex != -1 && cbxTinh.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = Truong_Bus.dataTable(Convert.ToInt32(cbxTinh.SelectedValue));
                    cbxTruong.DataSource = dt;
                    cbxTruong.DisplayMember = "tenTruong";
                    cbxTruong.ValueMember = "maTruong";
                }
            }
            catch
            {

            }
        }

        private void gctThiSinh_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && gvwThiSinh.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Bạn muốn xóa?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    _thiSinh.RemoveAt(gvwThiSinh.FocusedRowHandle);
                    gvwThiSinh.RefreshData();
                }
            }
        }

        private void gvwThiSinh_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnExportExcel_Click(object sender, EventArgs e)
        //{
        //    long maKT = Convert.ToInt64(cbxKyThi.SelectedValue);
        //    string maTruong = cbxTruong.SelectedValue.ToString();
        //    int maID = ThiSinh_Bus.getMaxID(DateTime.Now.Year.ToString());
        //    if (maID < 0)
        //    {
        //        return;
        //    }
        //    foreach (var item in _thiSinh)
        //    {
        //        item.MaKyThi = maKT;
        //        item.MaTruong = maTruong;
        //        item.SoBaoDanh = ThiSinh_Bus.getID(DateTime.Now.Year.ToString(), ++maID);
        //    }
        //    if (ThiSinh_Bus.Them(_thiSinh))
        //    {
        //        XtraMessageBox.Show("Thành công");
        //        _thiSinh.Clear();
        //        gvwThiSinh.RefreshData();
        //    }
        //    else
        //    {
        //        XtraMessageBox.Show("Thất bại");
        //    }
        //}

      
         

    }
}